package com.mes.controller;

import com.mes.model.Material;
import com.mes.repository.MaterialRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/materials")
class MaterialController {

    @Autowired
    private MaterialRepository materialRepository;

    @GetMapping
    public List<Material> getAllMaterials() {
        return materialRepository.findAll();
    }

    @PostMapping
    public Material addMaterial(@Valid @RequestBody Material material) {
        return materialRepository.save(material);
    }

    @DeleteMapping("/{id}")
    public void deleteMaterial(@PathVariable Long id) {
        materialRepository.deleteById(id);
    }

    @DeleteMapping
    public void deleteAllMaterials() {
        materialRepository.deleteAll();
    }
}
